import sys
import pygame

from pygame_Alian.bullet import Bullet


def check_key_arror(t, k, s):
    if t == pygame.KEYDOWN:
        if k == pygame.K_LEFT:
            s.move_left()
            s.moving = True
        elif k == pygame.K_RIGHT:
            s.move_right()
            s.moving = True
        elif k == pygame.K_UP:
            s.move_up()
            s.moving = True
        elif k == pygame.K_DOWN:
            s.move_down()
            s.moving = True
    elif t == pygame.KEYUP:
        if (k == pygame.K_LEFT or k == pygame.K_RIGHT or
            k == pygame.K_UP   or k == pygame.K_DOWN):
            s.moving = False

def check_key_fire(t, k, s):
    if t == pygame.KEYDOWN and k == pygame.K_SPACE:
        s.on_fire = True
    elif t == pygame.KEYUP and k == pygame.K_SPACE:
        s.on_fire = False


def check_event(settings, screen, ship, bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
            check_key_arror(event.type, event.key, ship)
            check_key_fire(event.type, event.key, ship)

    ship.keep_moving()

    if ship.on_fire:
        ship.fire()
